<?php

namespace App\Controllers\Diary;

use Phalcon\Validation;
use Phalcon\Validation\Validator\PresenceOf;
use Phalcon\Validation\Validator\Digit;
use App\Models\Diary\Comment;
use App\Models\Diary\Diary;
use App\Models\Diary\Topic;
class CommitComment extends \App\Libs\Base\BaseAction
{
    protected function beforeRun()
    {
        parent::beforeRun();
        $this->needLogin = TRUE;
    }

    /**
     * @return array    提交评论：
     *
     */
    public function execute()
    {
        if ($this->request->isPost()) {
            $oneComment = new Comment();

            $oneComment->uid = (int)$this->session->user->id;
//            $oneComment->uid = 1;
//            $oneComment->uid = $this->request->getPost('uid');
            if (!$this->request->has('did') || !$this->request->has('content')) {
                return \App\Libs\Views\Json::view(1, '缺少参数', null);
            }
            $oneComment->content = (string)$this->request->getPost('content');
            $oneComment->did = (int)$this->request->getPost('did');

            if ($this->request->has('fid')) {
                $oneComment->fid = $this->request->getPost('fid');
            } else {
                $oneComment->fid = 0;
            }
            $oneComment->ctime = date('Y-m-d H:i:s', time());

            //验证
            $validation = new Validation();
            //did
            $validation->add('did',
                new PresenceOf(
                    array(
                        'message' => '缺少did'
                    )
                )
            );
            $validation->add('did',
                new Digit(
                    array(
                        'message' => 'did只能是数字'
                    )
                )
            );
            //content
            $validation->add('content',
                new PresenceOf(
                    array(
                        'message' => '评论内容为空'
                    )
                )
            );

            if (count($messages = $validation->validate($oneComment)) != 0) {
                //打开注释即可返回原因
                $finalMsg = '';
//                foreach ($messages as $message) {
//                    $finalMsg .=' | '. $message;
//                }
//                return \App\Libs\Views\Json::view(1, $finalMsg, null);
                return \App\Libs\Views\Json::view(1, '评论失败', null);
            }

            if ($oneComment->create() == false) {
                return \App\Libs\Views\Json::view(1, '失败,请重试', null);
            } else {
                //增加活动评论数：
                $matchDiray = Diary::find([
                    'conditions'  => 'id=?1',
                    'bind' => [1=>$oneComment->did]
                ]);
                $matchDiray->diary_num +=1;
                $matchDiray->update();
                $matchTopic = Topic::find([
                    'conditions' =>'period=?1',
                    'bind' =>[1=>$matchDiray->period]
                ]);
                $matchTopic->topic_sum_comment = (int)$matchTopic->topic_sum_comment+1;
                $matchTopic->update();
                return \App\Libs\Views\Json::view(0, '评论成功', null);
            }
        } else {
            return \App\Libs\Views\Json::view(1, 'wrong method', null);
        }
    }

}
